using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace AppManagement.Repositories
{
	#region using
	using AppManagement.Models;
	using AppManagement.Helpers;
	#endregion

	public class VeRepository
	{
		public static List<Ve> SelectAll()
		{
			try
			{
				return DataContext.Instance.Ve.ToList();
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			return new List<Ve>();
		}

		public static Ve SelectByID(int mave)
		{
			try
			{
				return DataContext.Instance.Ve.FirstOrDefault(item => item.MaVe == mave );
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			return null;
		}

		public static bool Insert(Ve obj)
		{
			try
			{
				DataContext.Instance.Ve.AddObject(obj);
				DataContext.Instance.SaveChanges();
				return true;
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
				return false;
		}

		public static bool Delete(int mave)
		{
			try
			{
				var delitem = DataContext.Instance.Ve.FirstOrDefault(item => item.MaVe == mave );
				DataContext.Instance.Ve.DeleteObject(delitem);
				DataContext.Instance.SaveChanges();
				return true;
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
				return false;
		}

		public static bool Save()
		{
			try
			{
				DataContext.Instance.SaveChanges();
				return true;
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
				return false;
		}

		public static List<Ve> RetrieveByID(int mave)
		{
			try
			{
				return (from item in DataContext.Instance.Ve where  item.MaVe == mave  select item).ToList();
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			return new List<Ve>();
		}

		public static List<Ve> SelectByMaKhachHang(int makhachhang)
		{
			try
			{
			var lstItem = (from item in DataContext.Instance.Ve where item.MaKhachHang == makhachhang select item).ToList();
			return lstItem;
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			return new List<Ve>();
		}

		public static List<Ve> SelectByMaChiTietLichHoatDong(int machitietlichhoatdong)
		{
			try
			{
			var lstItem = (from item in DataContext.Instance.Ve where item.MaChiTietLichHoatDong == machitietlichhoatdong select item).OrderBy(n => n.SoGhe).ToList();
			return lstItem;
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			return new List<Ve>();
		}

	}

}
